package cn.springcloud.fix.demo.future;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class FutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService exc = Executors.newFixedThreadPool(5);

        List<Future<Integer>> futureList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Callable<Integer> callable = () -> {
                System.out.println("开始睡眠");
                Thread.sleep(5000);
                System.out.println("睡眠结束");
                return new Random().nextInt();
            };
            System.out.println("开始提交任务");
            Future<Integer> future = exc.submit(callable);
            futureList.add(future);
        }

        for (Future<Integer> future : futureList) {
            Integer a = future.get();
            System.out.println(a);
        }

        System.out.println("shutdown");
        exc.shutdown();
    }
}
